<?php
class DatabaseObject{
	/*	Description: Connects to a mySQL database
	*	Parameters:
	* 	$dbhost - hostname
	* 	$dbuser - username
	* 	$dbpass - password
	*/
	public function connect($dbhost, $dbuser, $dbpass, $dbname){
		$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
		mysql_select_db($dbname) or die ('Unable to select ' . $dbname); //+$dbname
	}

	/*	Description: Closes a mySQL database connection
	 *	Parameters: None
	 */
	public function close($conn){
		mysql_close($conn);
	}

	/*	Description: Adds a restaurant to the database
	 *	Parameters:
	 * 	$name - restaurant name
	 * 	$address - restaurant address
	 *  $phone - restaurant phone number
	 *  $category - restaurant category
	 */
	public function addRestaurant($name,$address,$latitude,$longitude,$phone,$category){
		$addRestaurantQuery = "INSERT INTO restaurant VALUES('','$name','$address','$latitude','$longitude','$phone','$category')";
		mysql_query($addRestaurantQuery) or die(mysql_error());
	}

	public function dump(){
		$dumpQuery = "SELECT * FROM restaurant";
		$result = mysql_query($dumpQuery) or die(mysql_error());

		// Printing results in HTML
		echo "<table>\n";
		while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
			echo "\t<tr>\n";
			foreach ($line as $col_value) {
				echo "\t\t<td>$col_value</td>\n";
			}
			echo "\t</tr>\n";
		}
		echo "</table>\n";
	}

	/*
	 *  Description: Removes a restaurant from the database
	 *	Parameters:
	 * 	$name - restaurant name
	 */
	public function removeRestaurant($name){
		$removeRestaurantQuery = "DELETE restaurant, menu_item FROM restaurant, menu_item WHERE restaurant.name = '$name' AND restaurant.id = menu_item.restaurantID";
		mysql_query($removeRestaurantQuery) or die(mysql_error());
	}

	/*
	 * 	Description: Returns a list of menu items offered by a restaurant based on the amount of money you have.
	 * 				 The result returns values between the minimum and maximum amount you enter.
	 * 	Parameters:
	 * 	$amount - amount of money
	 */
	public function getLunchByPrice($min,$max){
		$getRestaurantQuery = "SELECT DISTINCT restaurant.* FROM restaurant, menu_item WHERE menu_item.price <= $max AND menu_item.price >= $min AND menu_item.restaurantID = restaurant.id";
		$restaurantResult = mysql_query($getRestaurantQuery) or die(mysql_error());
		$restaurantObj = $this->toJSON($restaurantResult);

		$getMenuItemQuery = "SELECT DISTINCT menu_item.* FROM restaurant, menu_item WHERE menu_item.price <= $max AND menu_item.price >= $min AND menu_item.restaurantID = restaurant.id";
		$menuItemResult = mysql_query($getMenuItemQuery) or die(mysql_error());
		$menuItemObj = $this->toJSON($menuItemResult);

		$result = array($restaurantObj, $menuItemObj);
		return $result;
	}
	
	/*
	 * 	Description: Returns a list of menu items offered by a restaurant based on the category of food.
	 * 	Parameters:
	 * 	$amount - amount of money
	 */
	public function getLunchByCategory($min,$max,$category){
		$restaurantQuery = "SELECT DISTINCT restaurant.* FROM restaurant, menu_item WHERE menu_item.price <= $max AND menu_item.price >= $min AND restaurant.category = '$category' AND menu_item.restaurantID = restaurant.id";
		$restaurantResult = mysql_query($restaurantQuery) or die(mysql_error());
		$restaurantObj = $this->toJSON($restaurantResult);
		
		$menuItemQuery = "SELECT DISTINCT menu_item.* FROM restaurant, menu_item WHERE menu_item.price <= $max AND menu_item.price >= $min AND restaurant.category = '$category' AND menu_item.restaurantID = restaurant.id";
		$menuItemResult = mysql_query($menuItemQuery) or die(mysql_error());
		$menuItemObj = $this->toJSON($menuItemResult);
		
		$result = array($restaurantObj, $menuItemObj);
		return $result;
	}
	
	/*
	 * 	Description: Returns a list of all the restaurants
	 * 	Parameters: N/A
	 */
	public function getAllRestaurants(){
		$qry = "SELECT * FROM restaurant";
		$result = mysql_query($qry) or die(mysql_error());
		
		$jsonObj = $this->toJSON($result);
		 return $jsonObj;
	}
	
	/*
	 * 	Description: Converts a mySQL result into a JSON object
	 * 	Parameters:
	 * 	$result - mySQL result
	 */
	public function toJSON($result){
		$encodable = array();
		while($obj = mysql_fetch_object($result)){$encodable[] = $obj;}
		$jsonObj = json_encode($encodable);
		return $jsonObj;
	}

	/*
	 * 	Description: Inserts a menu item into the database
	 * 	Parameters:
	 * 	$restaurant_id - id corresponding to a restaurant
	 * 	$name - name of menu item
	 * 	$price - price of menu item
	 */
	public function addMenuItem($restaurant_id,$name,$price,$picture){
		$addMenuItemQuery = "INSERT INTO menu_item VALUES('','$restaurant_id','$name','$price','$picture')";
		mysql_query($addMenuItemQuery) or die(mysql_error());
	}
	
	public function removeMenuItem($restaurantID, $menuItemName){
		$removeRestaurantQuery = "DELETE FROM menu_item WHERE menu_item.name = '$menuItemName' AND menu_item.restaurantID = $restaurantID";
		mysql_query($removeRestaurantQuery) or die(mysql_error());
	}
}
?>